package com.feng.chat.controller;

import com.feng.chat.dto.Result;
import com.feng.chat.service.IArticleAnnexService;
import com.feng.chat.service.IArticleClassService;
import com.feng.chat.service.IArticleService;
import com.feng.chat.service.IArticleTagService;
import com.feng.chat.utils.GetUserUtil;
import com.feng.chat.vo.TagVO;
import com.feng.chat.vo.AnnexUploadVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/note")
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    @Autowired
    private IArticleClassService articleClassService;

    @Autowired
    private IArticleTagService articleTagService;

    @Autowired
    private IArticleAnnexService articleAnnexService;

    /**
     * 获取笔记分类列表
     *
     * @param request
     * @return
     */
    @GetMapping("/class/list")
    public Result classList(HttpServletRequest request) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = articleClassService.classList(userId);
        return result;
    }

    /**
     * 获取笔记标签列表
     *
     * @param request
     * @return
     */
    @GetMapping("/tag/list")
    public Result tagList(HttpServletRequest request) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = articleTagService.tagList(userId);
        return result;
    }

    /**
     * 获取笔记列表
     *
     * @param request
     * @param page
     * @param keyword
     * @param find_type 1:获取近期日记  2:获取星标日记  3:获取指定分类文章  4:获取指定标签文章 5:获取已删除文章 6:关键词搜索
     * @param cid
     * @return
     */
    @GetMapping("/article/list")
    public Result articleList(HttpServletRequest request, Integer page, String keyword, Integer find_type, Integer cid) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = articleService.articleList(userId, page, keyword, find_type, cid);
        return result;
    }

    /**
     * 编辑分类
     *
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/class/editor")
    public Result classEditor(HttpServletRequest request, @RequestBody Map<String, String> map) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = articleClassService.classEditor(userId, Integer.parseInt(map.get("class_id")), map.get("class_name"));
        return result;
    }

    /**
     * 编辑标签
     *
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/tag/editor")
    public Result tagEditor(HttpServletRequest request, @RequestBody Map<String, String> map) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = articleTagService.tagEditor(userId, Integer.parseInt(map.get("tag_id")), map.get("tag_name"));
        return result;
    }

    /**
     * 删除分类
     *
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/class/delete")
    public Result classDelete(HttpServletRequest request, @RequestBody Map<String, Integer> map) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = articleClassService.classDelete(userId, map.get("class_id"));
        return result;
    }

    /**
     * 删除标签
     *
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/tag/delete")
    public Result tagDelete(HttpServletRequest request, @RequestBody Map<String, Integer> map) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = articleTagService.tagDelete(userId, map.get("tag_id"));
        return result;
    }

    /**
     * 上传笔记图片
     *
     * @param image
     * @return
     */
    @PostMapping("/article/upload/image")
    public Result uploadImage(MultipartFile image) {
        Result result = articleService.uploadImage(image);
        return result;
    }

    /**
     * 编辑笔记
     *
     * @param request
     * @param map
     * @return
     */
    @PostMapping("/article/editor")
    public Result articleEditor(HttpServletRequest request, @RequestBody Map<String, String> map) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = articleService.articleEditor(userId, Integer.parseInt(map.get("article_id")), Integer.parseInt(map.get("class_id")), map.get("content"), map.get("md_content"), map.get("title"));
        return result;
    }

    /**
     * 查询笔记详细信息
     *
     * @param article_id
     * @return
     */
    @GetMapping("/article/detail")
    public Result articleDetail(Integer article_id) {
        Result result = articleService.articleDetail(article_id);
        return result;
    }

    /**
     * 修改笔记标签
     *
     * @param tagVO
     * @return
     */
    @PostMapping("/article/tag")
    public Result articleTag(@RequestBody TagVO tagVO) {
        Result result = articleTagService.articleTag(tagVO);
        return result;
    }


    /**
     * 上传笔记附件
     *
     * @param request
     * @param annexUploadVO
     * @return
     */
    @PostMapping("/annex/upload")
    public Result annexUpload(HttpServletRequest request, AnnexUploadVO annexUploadVO) {
        Integer userId = GetUserUtil.getUserId(request);
        Result result = articleAnnexService.annexUpload(userId, annexUploadVO);
        return result;
    }

    /**
     * 附件下载
     *
     * @param response
     * @param annex_id
     * @throws Exception
     */
    @GetMapping("/annex/download")
    public void annexDownload(HttpServletResponse response, Integer annex_id) throws Exception {
        articleAnnexService.annexDownload(response, annex_id);
    }

    /**
     * 删除附件
     *
     * @param map
     * @return
     */
    @PostMapping("/annex/delete")
    public Result annexDelete(@RequestBody Map<String, Integer> map) {
        Result result = articleAnnexService.annexDelete(map.get("annex_id"));
        return result;
    }

    /**
     * 设置星标笔记
     *
     * @param map
     * @return
     */
    @PostMapping("/article/asterisk")
    public Result articleAsterisk(@RequestBody Map<String, Integer> map) {
        Result result = articleService.articleAsterisk(map.get("article_id"), map.get("type"));
        return result;
    }

    /**
     * 删除笔记
     * @param map
     * @return
     */
    @PostMapping("/article/delete")
    public Result articleDelete(@RequestBody Map<String, Integer> map){
        Result result = articleService.articleDelete(map.get("article_id"));
        return result;
    }

    /**
     * 恢复笔记
     * @param map
     * @return
     */
    @PostMapping("/article/recover")
    public Result articleRecover(@RequestBody Map<String, Integer> map){
        Result result = articleService.articleRecover(map.get("article_id"));
        return result;
    }

    /**
     * 永久删除笔记回收站的笔记
     * @param map
     * @return
     */
    @PostMapping("/article/forever/delete")
    public Result foreverDelete(@RequestBody Map<String, Integer> map){
        Result result = articleService.foreverDelete(map.get("article_id"));
        return result;
    }

    /**
     * 笔记附件回收站列表
     * @param request
     * @return
     */
    @GetMapping("/annex/recover/list")
    public Result recoverList(HttpServletRequest request){
        Integer userId = GetUserUtil.getUserId(request);
        Result result = articleAnnexService.recoverList(userId);
        return result;
    }

    /**
     * 恢复笔记附件
     * @param map
     * @return
     */
    @PostMapping("/annex/recover")
    public Result annexRecover(@RequestBody Map<String, Integer> map){
        Result result = articleAnnexService.annexRecover(map.get("annex_id"));
        return result;
    }

    /**
     * 永久刪除笔记附件
     * @param map
     * @return
     */
    @PostMapping("/annex/forever/delete")
    public Result annexForeverDelete(@RequestBody Map<String, Integer> map){
        Result result = articleAnnexService.annexForeverDelete(map.get("annex_id"));
        return result;
    }
}
